<template>
  <div class="app-container">
    <div class="add">
      <el-form label-width="120px">
        <el-form-item label="用户名">
          <el-input v-model="user.userName" />
        </el-form-item>
        <el-form-item label="用户密码">
          <el-input v-model="user.userPassword" controls-position="right" show-password />
        </el-form-item>
        <el-form-item label="性别">
          <el-radio-group v-model="user.userSex" :disabled="select">
            <el-radio label="男">男</el-radio>
            <el-radio label="女">女</el-radio>
          </el-radio-group>

        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input v-model="user.realName" :disabled="select" />
        </el-form-item>
        <el-form-item label="身份证号码">
          <el-input v-model="user.userIdcard" :disabled="select" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="user.userTel" />
        </el-form-item>
        <el-form-item label="用户登录权限">
          <el-switch
            v-model="user.permission"
            active-text="开启"
            inactive-text="禁止"
            @change="updatePermission(scope.row.permission,scope.row.userId)"
          />
        </el-form-item>
        <el-form-item>
          <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">保存</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import ImageCropper from '@/components/ImageCropper'
import PanThumb from '@/components/PanThumb'
import user from '@/api/rental/user'

// 抽取出对象
const defaultForm = {
  userId: '', // '员工id',
  userName: '', // '帐号',
  userPassword: '', // '密码',
  realName: '', // '员工的姓名',
  userIdcard: '', // 身份证号码
  userSex: '', // 性别
  userTel: '', // 电话号码
  permission: '' // 登录权限
  // avatar: 'https://zhouhailiang003.oss-cn-beijing.aliyuncs.com/avatar/my01.jpg' // 默认图片地址
}

export default {
  // 注册组件
  components: { ImageCropper, PanThumb },
  data() {
    return {
      user: defaultForm, // 为对象赋值
      saveBtnDisabled: false, // 保存按钮是否禁用,
      BASE_API: process.env.VUE_APP_BASE_API, // 接口API地址,来自于.env.development 文件中的VUE_APP_BASE_API = 'http://127.0.0.1:8210'
      imagecropperShow: false, // 是否显示上传组件
      imagecropperKey: 0, // 上传组件id
      select: false // 是否可以修改
    }
  },
  watch: {
    $route(to, from) {
      console.log('watch $route')
      this.init()
    }
  },
  created() {
    console.log(this.$route.params)
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.fetchDataById(id)
      this.select = true
    }
  },
  methods: {

    // 保存或更新操作
    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (!this.teacher.id) {
        this.saveData()
      } else {
        this.updateData()
      }
    },

    // 保存
    saveData() {
      user.save(this.teacher).then(response => {
        return this.$message({
          type: 'success',
          message: '保存成功!'
        })
      }).then(resposne => {
        this.$router.push({ path: '/edu/teacher' })
      })
    },
    // 根据id更新记录
    updateData() {
      user.updateById(this.teacher).then(response => {
        return this.$message({
          type: 'success',
          message: '修改成功!'
        })
      }).then(resposne => {
        this.$router.push({ path: '/edu/teacher' })
      })
    },
    // 根据id查询记录
    fetchDataById(id) {
      user.getById(id).then(response => {
        this.user = response.data
      })
    },
    init() {
      if (this.$route.params && this.$route.params.id) {
        const id = this.$route.params.id
        this.fetchDataById(id)
      } else {
        // 使用对象拓展运算符，拷贝对象，而不是引用，
        // 否则新增一条记录后，defaultForm就变成了之前新增的teacher的值
        this.teacher = { ...defaultForm }
      }
    },
    // 上传成功后的回调函数,data是响应的数据
    cropSuccess(data) {
      console.log(data)
      this.imagecropperShow = false
      this.teacher.avatar = data.url
      // 上传成功后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    },
    // 关闭上传组件
    close() {
      this.imagecropperShow = false
      // 上传失败后，重新打开上传组件时初始化组件，否则显示上一次的上传结果
      this.imagecropperKey = this.imagecropperKey + 1
    }
  }

}
</script>
<style>
  .add{
    margin: 0 auto;
    width: 500px;
  }
</style>
